package com.cswnode.demo.service.impl;

import com.cswnode.demo.dao.EnterpriseDao;
import com.cswnode.demo.entity.Enterprise;
import com.cswnode.demo.enums.YesOrNoEnum;
import com.cswnode.demo.service.EnterpriseService;
import com.cswnode.demo.vo.EnterpriseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 企业表(Enterprise)表服务实现类
 *
 * @author makejava
 * @since 2021-01-26 11:08:01
 */
@Service
public class EnterpriseServiceImpl implements EnterpriseService {
    @Autowired
    private EnterpriseDao enterpriseDao;
    @Autowired
    private DataSourceTransactionManager transactionManager;

    /**
     * 通过ID查询单条数据
     *
     * @param entId 主键
     * @return 实例对象
     */
    @Override
    public Enterprise queryById(Long entId) {
        return this.enterpriseDao.queryById(entId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Enterprise> queryAllByLimit(int offset, int limit) {
        return this.enterpriseDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<EnterpriseVo> queryAllByVo(int offset, int limit, String entName, String grade) {
        return enterpriseDao.queryAllByVo(offset, limit, entName, grade);
    }

    @Override
    public List<Long> queryEntIdByName(String entName) {
        List<Enterprise> enterprises = enterpriseDao.queryByName(entName);
        if (enterprises != null){
            List<Long> entIds = new ArrayList<>();
            for (Enterprise enterpris : enterprises) {
                entIds.add(enterpris.getEntId());
            }
            return entIds;
        }
        return null;
    }

    @Override
    public int selectNum(String entName, String grade) {
        return enterpriseDao.selectNum(entName,grade);
    }

    /**
     * 新增数据
     *
     * @param enterprise 实例对象
     * @return 实例对象
     */
    @Override
    public Enterprise insert(Enterprise enterprise) {
        enterprise.setEntId(null);
        enterprise.setCreateTime(new Date());
        enterprise.setIsDelete(YesOrNoEnum.NO.getCode());
        this.enterpriseDao.insert(enterprise);
        return enterprise;
    }

    /**
     * 修改数据
     *
     * @param enterprise 实例对象
     * @return 实例对象
     */
    @Override
    public String update(Enterprise enterprise) {
        if (enterprise.getEntId() == null){
            return "修改失败！";
        }
        int update = this.enterpriseDao.update(enterprise);
        if (update == 1){
            return "修改成功！";
        }
        return "修改失败！";
    }

    /**
     * 通过主键删除数据
     *
     * @param entId 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(Long entId) {
        if (entId == null){
            return false;
        }
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setName("SomeTxName");
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try{
            enterpriseDao.deleteById(entId);
            enterpriseDao.deleteByPost(entId);
        }catch (Exception e){
            transactionManager.rollback(status);
            throw e;
        }
        return true;
    }
}